﻿using CommunityToolkit.Mvvm.ComponentModel;
using LongSCADA_WPF.Helpers;
using LongSCADA_WPF.Models;
using Masuit.Tools;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LongSCADA_WPF.ViewModels
{
    public partial class ShellViewModel:ObservableObject
    {
        public ShellViewModel()
        {
          //InitData();
        }

        /// <summary>
        /// 初次进行数据库的 CodeFirst，创建表及初始化数据
        /// </summary>
        /// <exception cref="NotImplementedException"></exception>
        private void InitData()
        {
            // 建库
            //SqlSugarHelper.Db.DbMaintenance.CreateDatabase();
            //建 user 表
            // SqlSugarHelper.Db.CodeFirst.InitTables<UserModel>();
            //建 menu 表
            //SqlSugarHelper.Db.CodeFirst.InitTables<Menu>();
            //建 ScadaReadData 表
            //SqlSugarHelper.Db.CodeFirst.InitTables<ScadaReadData>();
            //建 FormulaEntity 表
            SqlSugarHelper.Db.CodeFirst.InitTables<FormulaEntity>();
            //插入数据
            // CreatUserTable();
            //CreatMenuTable();
            //CreatScadaReadDataTable();
            CreatFormulaEntityDataTable();
        }

        private void CreatFormulaEntityDataTable()
        {
            List<FormulaEntity> tabelList = new List<FormulaEntity>();
            for (int i = 0; i < 10; i++)
            {
                FormulaEntity formulaEntity = new FormulaEntity() 
                {
                    Name = "Formula"+i,
                    Description = "Test Formula",
                    IsSelected = false,
                    DegreasingSetPressureUpperLimit = GetRandomFloat(44, 54),
                    DegreasingSetPressureLowerLimit = GetRandomFloat(5, 26),
                    RoughWashingSprayPumpOverloadUpperLimit = GetRandomFloat(0, 22),
                    RoughWashingLevelLowerLimit = GetRandomFloat(12, 34),
                    CeramicCoatingSprayPumpOverloadUpperLimit = GetRandomFloat(32, 53),
                    FineWashingSprayPumpOverloadUpperLimit = GetRandomFloat(11, 43),
                    FineWashingLevelLowerLimit = GetRandomFloat(15, 21),
                    MoistureFurnaceTemperatureUpperLimit = GetRandomFloat(77, 100),
                    MoistureFurnaceTemperatureLowerLimit = GetRandomFloat(25, 66),
                    CoolingRoomCentrifugalFanOverloadUpperLimit = GetRandomFloat(32, 44),
                    CuringOvenTemperatureUpperLimit = GetRandomFloat(31, 44),
                    CuringOvenTemperatureLowerLimit = GetRandomFloat(15, 21),
                    ConveyorSetSpeed = GetRandomFloat(77, 100),
                    ConveyorSetFrequency = GetRandomFloat(25, 66),
                    CreateTime =DateTime.Now.AddMinutes(GetRandomFloat(25, 200)),
                    UpdateTime =DateTime.Now.AddMinutes(GetRandomFloat(30, 210))
                };
               tabelList.Add(formulaEntity);
                
            }
            SqlSugarHelper.Db.Insertable(tabelList).ExecuteCommand();
        }
        private void CreatScadaReadDataTable()
        {
            List<ScadaReadData> tabelList = new List<ScadaReadData>();
            for (int i = 0; i < 100; i++)
            {
                ScadaReadData scadaReadData = new ScadaReadData() 
                {
                    DegreasingSprayPumpPressure = GetRandomFloat(33, 66),
                    DegreasingPhValue = GetRandomFloat(5, 26),
                    RoughWashSprayPumpPressure = GetRandomFloat(16, 100),
                    PhosphatingSprayPumpPressure = GetRandomFloat(44, 54),
                    PhosphatingPhValue = GetRandomFloat(5, 26),
                    FineWashSprayPumpPressure= GetRandomFloat(0, 22),
                    MoistureFurnaceTemperature= GetRandomFloat(12, 34),
                    CuringFurnaceTemperature = GetRandomFloat(32, 53),
                    FactoryTemperature= GetRandomFloat(11, 43),
                    FactoryHumidity = GetRandomFloat(15, 21),
                    ProductionCount = GetRandomFloat(77, 100),
                    DefectiveCount = GetRandomFloat(25, 66),
                    ProductionPace= GetRandomFloat(32, 44),
                    AccumulatedAlarms= GetRandomFloat(31, 44),
                    CreateTime =DateTime.Now.AddMinutes(GetRandomFloat(25, 200)),
                    UpdateTime =DateTime.Now.AddMinutes(GetRandomFloat(30, 210))
                };
               tabelList.Add(scadaReadData);
                
            }
            SqlSugarHelper.Db.Insertable(tabelList).ExecuteCommand();
        }

        private float GetRandomFloat(float min, float max)
        {
           return (float)(new Random().NextDouble()*(max-min));
        }

        private static void CreatMenuTable()
        {
            List<Menu> tabelList = new List<Menu>();
            tabelList.Add(new Menu()
            {
                MenuName = "首页",
                Icon = "Home",
                View = "IndexView",
                Role = 0,
                Sort = 1,
            });
            tabelList.Add(new Menu()
            {
                MenuName = "设备总控",
                Icon = "Devices",
                View = "DeviceView",
                Role = 0,
                Sort = 2,
            });
            tabelList.Add(new Menu() {
                MenuName = "配方管理",
                Icon = "AirFilter",
                View = "FormulaView",
                Role = 0,
                Sort = 3,
            });
            tabelList.Add(new Menu()
            {
                MenuName = "参数管理",
                Icon = "AlphabetCBoxOutline",
                View = "ParamsView",
                Role = 0,
                Sort = 4,
            });
            tabelList.Add(new Menu()
            {
                MenuName = "数据查询",
                Icon = "DataUsage",
                View = "DataQueryView",
                Role = 0,
                Sort = 5,
            });
            tabelList.Add(new Menu() {
                MenuName = "数据趋势",
                Icon = "ChartFinance",
                View = "ChartView",
                Role = 0,
                Sort = 6,
            });
            tabelList.Add(new Menu()
            {
                MenuName = "日志管理",
                Icon = "NotebookOutline",
                View = "LogView",
                Role = 0,
                Sort = 8,
            });
            tabelList.Add(new Menu()
            {
                MenuName = "用户管理",
                Icon = "UserMultipleOutline",
                View = "UserView",
                Role = 0,
                Sort = 9,
            });
            
            //插入
            SqlSugarHelper.Db.Insertable(tabelList).ExecuteCommand();
        }
        private static void CreatUserTable()
        {
            List<UserModel> tabelList = new List<UserModel>();
            tabelList.Add(new UserModel() { UserName = "admin", PassWord = "root", Role = 0 });
            tabelList.Add(new UserModel() { UserName = "visitor", PassWord = "1234", Role = 1 });
            tabelList.Add(new UserModel() { UserName = "visitor", PassWord = "1111", Role = 1 });
            //插入
            SqlSugarHelper.Db.Insertable(tabelList).ExecuteCommand();
        }
    }
}
